﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Registered_Student_Management
{
    public partial class F_Main : Form
    {
        string connection_string =  "Provider=Microsoft.Jet.OleDb.4.0;Data Source=Database.mdb;Jet OLEDB:Database Password=51002201";

        int lvl;
        int phanquyen;
        int center;
        int ChangeRatio;
        
        bool gettotextbox = false;

        public F_Main()
        {
            InitializeComponent();
        }

        private void F_Main_Load(object sender, EventArgs e)
        {
 
            GB_Login.Visible = true;
            GB_Main.Visible = false;
            GB_TrangChu.Visible = false;
            GB_HR.Visible = false;
            GB_Class.Visible = false;
            GB_Student.Visible = false;
            GB_Course.Visible = false;
        }
        private void BT_Login_Click(object sender, EventArgs e)
        {
            
            try
            {
                OleDbConnection cnlogin = new OleDbConnection();
                cnlogin.ConnectionString = connection_string;
                cnlogin.Open();
                OleDbCommand commandlogin = new OleDbCommand();
                commandlogin.Connection = cnlogin;
                commandlogin.CommandText = "select * from [user] where username = @username and password = @password";
                commandlogin.Parameters.AddWithValue("@username", TB_Name.Text);
                commandlogin.Parameters.AddWithValue("@password", TB_Pass.Text);
                OleDbDataAdapter adapt = new OleDbDataAdapter(commandlogin);
                DataTable data = new DataTable();
                adapt.Fill(data);
/*              OleDbDataReader reader = commandlogin.ExecuteReader();
//              while (reader.Read())
//              {*/
                if (data.Rows.Count >0)
                {
                    DataRow dr = data.Rows[0];
                    lvl = int.Parse(dr["level"].ToString());
                    GB_Login.Visible = false;
                    GB_Main.Visible = true;
                    GB_TrangChu.Visible = true;
                    GB_HR.Visible = false;
                    GB_Class.Visible = false;
                    GB_Student.Visible = false;
                    MessageBox.Show("Đăng Nhập Thành Công");
                    
                }
                else
                {
                    if (MessageBox.Show("Đăng Nhập Thất Bại, Bạn có muốn Đăng Nhập lại không?", "Đăng Nhập", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        TB_Name.Focus();
                    }
                    else
                    {
                        Close();
                        System.Windows.Forms.Application.Exit();
                    }
                }
//              }
                cnlogin.Close();
                cnlogin.Dispose();
                cnlogin = null;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
           
        }

        private void HomeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GB_Login.Visible = false;
            GB_Main.Visible = true;
            GB_TrangChu.Visible = true;
            GB_HR.Visible = false;
            GB_Class.Visible = false;
            GB_Student.Visible = false;
        }

        private void StudentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GB_Login.Visible = false;
            GB_Main.Visible = true;
            GB_TrangChu.Visible = false;
            GB_HR.Visible = false;
            GB_Class.Visible = false;
            GB_Student.Visible = true;
            GB_Course.Visible = false;
        }

        private void HRToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GB_Login.Visible = false;
            GB_Main.Visible = true;
            GB_HR.Visible = true;
            GB_TrangChu.Visible = false;
            GB_Class.Visible = false;
            GB_Student.Visible = false;
            GB_Course.Visible = false;
        }

        private void ClassToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GB_Login.Visible = false;
            GB_Main.Visible = true;
            GB_TrangChu.Visible = false;
            GB_HR.Visible = false;
            GB_Class.Visible = true;
            GB_Student.Visible = false;
            GB_Course.Visible = false;
        }

        private void CourseToolStripMenuItemm_Click(object sender, EventArgs e)
        {
            GB_Login.Visible = false;
            GB_Main.Visible = true;
            GB_TrangChu.Visible = false;
            GB_HR.Visible = false;
            GB_Class.Visible = false;
            GB_Student.Visible = false;
            GB_Course.Visible = true;
        }

//--------------------------------------------------------GB_Student-----------------------------------------------------//
        //-----------------------------------------------------------------------------------------------------//

        private void GB_Student_VisibleChanged(object sender, EventArgs e)
        {
            RBT_tatcaHV_CheckedChanged(sender, e);
            DT_ngaysinhHV.Value = DateTime.Now;
        }

        private void DGV_danhsachHV_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (gettotextbox == true)
                {
                    try
                    {
                        OleDbConnection cnData = new OleDbConnection();
                        cnData.ConnectionString = connection_string;
                        OleDbCommand commandData = new OleDbCommand();
                        commandData.Connection = cnData;
                        commandData.CommandText = "select gender from student";
                        OleDbDataAdapter adapt = new OleDbDataAdapter(commandData);
                        DataSet ds = new DataSet();
                        adapt.Fill(ds);
                        CB_gioitinhHV.DataSource = ds.Tables[0];
                        CB_gioitinhHV.DisplayMember = "gender";
                        CB_gioitinhHV.ValueMember = "gender";
                    }
                    catch (Exception Ex)
                    {
                        MessageBox.Show(Ex.Message);
                    }
                    int i = DGV_danhsachHV.CurrentCell.RowIndex;
                    TB_maHV.Text = DGV_danhsachHV.Rows[i].Cells[0].Value.ToString();
                    TB_tenHV.Text = DGV_danhsachHV.Rows[i].Cells[1].Value.ToString();
                    CB_gioitinhHV.Text = DGV_danhsachHV.Rows[i].Cells[2].Value.ToString();
                    DT_ngaysinhHV.Text = DGV_danhsachHV.Rows[i].Cells[3].Value.ToString();
                    TB_sdtHV.Text = DGV_danhsachHV.Rows[i].Cells[5].Value.ToString();
                    TB_nghenghiepHV.Text = DGV_danhsachHV.Rows[i].Cells[4].Value.ToString();
                    TB_diachiHV.Text = DGV_danhsachHV.Rows[i].Cells[6].Value.ToString();
                    TB_emailHV.Text = DGV_danhsachHV.Rows[i].Cells[7].Value.ToString();

                }
                else
                {
                    try
                    {
                        OleDbConnection cnData = new OleDbConnection();
                        cnData.ConnectionString = connection_string;
                        OleDbCommand commandData = new OleDbCommand();
                        commandData.Connection = cnData;
                        commandData.CommandText = "select status from job ";
                        OleDbDataAdapter adapt = new OleDbDataAdapter(commandData);
                        DataSet ds = new DataSet();
                        adapt.Fill(ds);
                        CB_trangthaiHV.DataSource = ds.Tables[0];
                        CB_trangthaiHV.DisplayMember = "status";
                        CB_trangthaiHV.ValueMember = "status";
                    }
                    catch (Exception Ex)
                    {
                        MessageBox.Show(Ex.Message);
                    }
                    int j = DGV_danhsachHV.CurrentCell.RowIndex;
                    TB_matheHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[0].Value.ToString();
                    TB_maHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[1].Value.ToString();
                    TB_tenHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[2].Value.ToString();
                    TB_malopHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[3].Value.ToString();
                    TB_tenlopHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[4].Value.ToString();
                    CB_trangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[5].Value.ToString();
                    DT_ngaykhoitaoHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[6].Value.ToString();
                    TB_nguoikhoitaoHVtrangthaiHV.Text = DGV_danhsachHV.Rows[j].Cells[7].Value.ToString();

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void RBT_tatcaHV_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_hv(sender,e);
            PN_themHV.Visible = true;
            PN_themHV.Enabled = true;
            gettotextbox = true;
            PN_trangthaiHV.Visible = false;
            BT_doitrangthaiHV.Visible = false;
            BT_doithongtinHV.Visible = true;
            BT_timkiemHV.Enabled = true;
        }

        private void RBT_danghocHV_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_hvdanghoc(sender, e);
            PN_themHV.Enabled = false;
            gettotextbox = false;
            ChangeRatio = 1;
            PN_themHV.Visible = false;
            PN_trangthaiHV.Visible = true;
            BT_doitrangthaiHV.Visible = true;
            BT_doithongtinHV.Visible = false;
            BT_timkiemHV.Enabled = false;
        }
        private void RBT_waitingHV_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_hvdangcho(sender, e);
            PN_themHV.Enabled = false;
            gettotextbox = false;
            ChangeRatio = 2;
            PN_themHV.Visible = false;
            PN_trangthaiHV.Visible = true;
            BT_doitrangthaiHV.Visible = true;
            BT_doithongtinHV.Visible = false;
            BT_timkiemHV.Enabled = false;
        }
        private void RBT_finishHV_CheckedChanged(object sender, EventArgs e)
        {
            load_datagrid_hvhoanthanh(sender, e);
            PN_themHV.Enabled = false;
            gettotextbox = false;
            ChangeRatio = 3;
            PN_themHV.Visible = false;
            PN_trangthaiHV.Visible = true;
            BT_doitrangthaiHV.Visible = true;
            BT_doithongtinHV.Visible = false;
            BT_timkiemHV.Enabled = false;
        }    
        
        private void load_datagrid_hv(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from student";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_timkiemhv(object sender, EventArgs e)
        {
            try
            {
                if (TB_timkiemHV.Text == "")
                {
                    MessageBox.Show("Nhập Thông Tin Cần Tìm", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cn = new OleDbConnection();
                    cn.ConnectionString = connection_string;
                    OleDbCommand commandtimkiemHV = new OleDbCommand();
                    commandtimkiemHV.Connection = cn;
                    int ID = int.Parse(TB_timkiemHV.Text.ToString());
                    commandtimkiemHV.CommandText = "select * from student where id = " + ID;
                    OleDbDataAdapter adapt = new OleDbDataAdapter(commandtimkiemHV);
                    DataTable dt = new DataTable();
                    adapt.Fill(dt);
                    DGV_danhsachHV.DataSource = dt;
                    cn.Close();
                    cn.Dispose();
                    cn = null;
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_hvdanghoc(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from job where status = 'learning' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_hvtamdung(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from job where status = 'pause' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_hvdangcho(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from job where status = 'waiting' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_hvhoanthanh(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from job where status = 'finish' ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHV.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        

        private void BT_doithongtinHV_Click(object sender, EventArgs e)
        {
            try
            {
                if (TB_tenHV.Text == "" || CB_gioitinhHV.Text == "" || DT_ngaysinhHV.Text == "" || TB_sdtHV.Text == "" || TB_nghenghiepHV.Text == "" || TB_diachiHV.Text == "")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cnUpdate = new OleDbConnection();
                    cnUpdate.ConnectionString = connection_string;
                    cnUpdate.Open();
                    OleDbCommand cmdUpdateHV = new OleDbCommand();
                    cmdUpdateHV.Connection = cnUpdate;
                    int id = int.Parse(TB_maHV.Text.ToString());
                    string fullname = TB_tenHV.Text, gender = CB_gioitinhHV.Text, career = TB_nghenghiepHV.Text, phone = TB_sdtHV.Text, address = TB_diachiHV.Text,email = TB_emailHV.Text;
                    string birthday = DT_ngaysinhHV.Value.ToShortDateString();
                    cmdUpdateHV.CommandText = "UPDATE student SET fullname ='" + fullname + "',gender ='" + gender + "',career ='" + career + "',birthday ='" + birthday + "',phone ='" + phone + "',address ='" + address + "',email = '" + email +"' WHERE id = " + id;
                    cmdUpdateHV.ExecuteNonQuery();
                    cnUpdate.Close();
                    load_datagrid_hv(sender, e);
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_doitrangthaiHV_Click(object sender, EventArgs e)
        {
            try
            {
                if (TB_tenHVtrangthaiHV.Text == "" || TB_maHVtrangthaiHV.Text == "" || TB_malopHVtrangthaiHV.Text == "" || TB_tenlopHVtrangthaiHV.Text == "" || TB_nguoikhoitaoHVtrangthaiHV.Text == "" || DT_ngaykhoitaoHVtrangthaiHV.Text == "")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }
                
                else if (PN_themHV.Enabled == false && PN_trangthaiHV.Enabled == false)
                {
                    OleDbConnection cnUpdateTT = new OleDbConnection();
                    cnUpdateTT.ConnectionString = connection_string;
                    cnUpdateTT.Open();
                    OleDbCommand cmdUpdateTTHV = new OleDbCommand();
                    cmdUpdateTTHV.Connection = cnUpdateTT;
                    int id = int.Parse(TB_matheHVtrangthaiHV.Text.ToString());
                    int maHV = int.Parse(TB_maHVtrangthaiHV.Text.ToString());
                    int maLop = int.Parse(TB_malopHVtrangthaiHV.Text.ToString());
                    string tenHV = TB_tenHVtrangthaiHV.Text, tenLop = TB_tenlopHVtrangthaiHV.Text, creater = TB_nguoikhoitaoHVtrangthaiHV.Text;
                    string create_day = DT_ngaykhoitaoHVtrangthaiHV.Value.ToShortDateString();
                    string status = CB_trangthaiHV.Text;
                    cmdUpdateTTHV.CommandText = "UPDATE job SET id_student ='" + maHV + "', name_student ='" + tenHV + "',id_class ='" + maLop + "', name_class ='" + tenLop + "',status = '" + status + "',create_person ='" + creater + "',create_date ='" + create_day + "' WHERE id = " + id;
                    cmdUpdateTTHV.ExecuteNonQuery();
                    cnUpdateTT.Close();
                    if (ChangeRatio == 1)
                    {
                        load_datagrid_hvdangcho(sender, e);
                    }
                    else if (ChangeRatio == 2)
                    {
                        load_datagrid_hvdanghoc(sender, e);
                    }
                    else if (ChangeRatio == 3)
                    {
                        load_datagrid_hvhoanthanh(sender, e);
                    }
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        
        private void BT_timkiemHV_Click(object sender, EventArgs e)
        {
            gettotextbox = true;
            load_datagrid_timkiemhv(sender, e);
        }


//--------------------------------------------------------GB_class-----------------------------------------------------//
        //-----------------------------------------------------------------------------------------------------//

        private void GB_Class_VisibleChanged(object sender, EventArgs e)
        {
            RB_tatcacacLop_CheckedChanged(sender, e);
        }

        private void DGV_danhsachLop_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                OleDbConnection cnData = new OleDbConnection();
                cnData.ConnectionString = connection_string;
                OleDbCommand commandData1 = new OleDbCommand();
                OleDbCommand commandData2 = new OleDbCommand();
                commandData1.Connection = cnData;
                commandData2.Connection = cnData;
                commandData1.CommandText = "select id_teacher,name_teacher from class";
                commandData2.CommandText = "select time from class";
                OleDbDataAdapter adapt1 = new OleDbDataAdapter(commandData1);
                OleDbDataAdapter adapt2 = new OleDbDataAdapter(commandData2);
                DataSet ds1 = new DataSet();
                DataSet ds2 = new DataSet();
                adapt1.Fill(ds1);
                adapt2.Fill(ds2);

                CB_tengiaovien.DataSource = ds1.Tables[0];
                CB_tengiaovien.DisplayMember = "name_teacher";
                CB_tengiaovien.ValueMember = "id_teacher";


                CB_sobuoihoc.DataSource = ds2.Tables[0];
                CB_sobuoihoc.DisplayMember = "time";
                CB_sobuoihoc.ValueMember = "time";
               
                int i = DGV_danhsachLop.CurrentCell.RowIndex;
                TB_maLop.Text = DGV_danhsachLop.Rows[i].Cells[0].Value.ToString();
                TB_kyhieuLop.Text = DGV_danhsachLop.Rows[i].Cells[1].Value.ToString();
                TB_tenLop.Text = DGV_danhsachLop.Rows[i].Cells[2].Value.ToString();
                TB_soluonghvLop.Text = DGV_danhsachLop.Rows[i].Cells[3].Value.ToString();
                TB_gioihanhvLop.Text = DGV_danhsachLop.Rows[i].Cells[4].Value.ToString();
                CB_tengiaovien.Text = DGV_danhsachLop.Rows[i].Cells[6].Value.ToString();
                CB_sobuoihoc.Text = DGV_danhsachLop.Rows[i].Cells[7].Value.ToString();
                TB_khoahocLop.Text = DGV_danhsachLop.Rows[i].Cells[8].Value.ToString();
                int state = int.Parse(DGV_danhsachLop.Rows[i].Cells[9].Value.ToString());
                if (state == 0) ChB_tinhtrangLop.Checked = false;
                else if (state == 1) ChB_tinhtrangLop.Checked = true;
                
                int numStudent = int.Parse(TB_soluonghvLop.Text.ToString());
               
                int full = int.Parse(TB_gioihanhvLop.Text.ToString());

                if (numStudent == full)
                {
                    BT_moLopfull.Enabled = true;
                    ChB_tinhtrangLop.Checked = true;

                }
                else if (numStudent < full)
                {
                    BT_moLopfull.Enabled = false;
                    ChB_tinhtrangLop.Checked = false;
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
            
        }

        
        private void RB_tatcacacLop_CheckedChanged(object sender, EventArgs e)
        {
            
            load_datagrid_tatcalop(sender, e);
            BT_xoaLop.Enabled = false;
            BT_moLopfull.Enabled = false;
            BT_themHV.Enabled = false;
            TB_gioihanhvLop.Enabled = false;
            TB_kyhieuLop.Enabled = false;
            TB_soluonghvLop.Enabled = false;
            CB_tengiaovien.Enabled = true;
            CB_sobuoihoc.Enabled = true;
        }

        private void RB_dangmoLop_CheckedChanged(object sender, EventArgs e)
        {
            
            load_datagrid_lopdanghoc(sender, e);
            BT_xoaLop.Enabled = false;
            BT_moLopfull.Enabled = false;
            BT_themHV.Enabled = false;
            TB_gioihanhvLop.Enabled = false;
            TB_kyhieuLop.Enabled = false;
            TB_soluonghvLop.Enabled = false;
            CB_tengiaovien.Enabled = true;
            CB_sobuoihoc.Enabled = true;
        }

        private void RB_dukienLop_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                
                load_datagrid_lopdangcho(sender, e);
                BT_xoaLop.Enabled = true;
                BT_moLopfull.Enabled = false;
                BT_themHV.Enabled = true;
                TB_gioihanhvLop.Enabled = true;
                TB_kyhieuLop.Enabled = true;
                TB_soluonghvLop.Enabled = false;
                CB_tengiaovien.Enabled = true;
                CB_sobuoihoc.Enabled = true;
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        
        private void load_datagrid_tatcalop(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from class";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachLop.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_lopdanghoc(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from class where available = 1 ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachLop.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_lopdangcho(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from class where available = 0 ";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachLop.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_timkiemLop(object sender, EventArgs e)
        {
            try
            {
                if (TB_timkiemLop.Text == "")
                {
                    MessageBox.Show("Nhập Thông Tin Cần Tìm", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cn = new OleDbConnection();
                    cn.ConnectionString = connection_string;
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = cn;
                    int id = int.Parse(TB_timkiemLop.Text.ToString());
                    command.CommandText = "select * from class where id = " + id ;
                    OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                    DataTable dt = new DataTable();
                    adapt.Fill(dt);
                    DGV_danhsachLop.DataSource = dt;
                    cn.Close();
                    cn.Dispose();
                    cn = null;
                   
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_danhsachHV_Click(object sender, EventArgs e)
        {
            try
            {
                F_ListStudentOfClass obj = new F_ListStudentOfClass();
                obj.truyendata(TB_maLop.Text);
                obj.ShowDialog(this);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        

        private void BT_themHV_Click(object sender, EventArgs e)
        {
            try
            {
                if (ChB_tinhtrangLop.Checked == true)
                {
                    MessageBox.Show("Lớp Đã Đủ Học Viên", "Thông Báo");
                }
                else
                {
                    
                    F_NewStudent obj = new F_NewStudent();
                    obj.getIDclass(TB_maLop.Text);
                    obj.getNAMEclass(TB_kyhieuLop.Text);
                    obj.getIDcourse(TB_khoahocLop.Text);
                    obj.ShowDialog(this);
                    load_datagrid_lopdangcho(sender, e);
                }
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


        private void BT_xoaLop_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnDel = new OleDbConnection();
                cnDel.ConnectionString = connection_string;
                cnDel.Open();
                OleDbCommand cmdDelHV = new OleDbCommand();
                cmdDelHV.Connection = cnDel;
                int id = int.Parse(TB_maLop.Text.ToString());
                cmdDelHV.CommandText = "DELETE FROM class WHERE id = " + id;
                cmdDelHV.ExecuteNonQuery();
                cnDel.Close();
                load_datagrid_lopdangcho(sender, e);

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void BT_moLopfull_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnUpdateLop = new OleDbConnection();
                cnUpdateLop.ConnectionString = connection_string;
                cnUpdateLop.Open();
                OleDbCommand cmdUpdateTTHV = new OleDbCommand();
                cmdUpdateTTHV.Connection = cnUpdateLop;
                int id = int.Parse(TB_maLop.Text.ToString());
                cmdUpdateTTHV.CommandText = "UPDATE class SET available = 1 WHERE id = " + id;
                cmdUpdateTTHV.ExecuteNonQuery();
                cnUpdateLop.Close();
                load_datagrid_lopdanghoc(sender, e);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        
        private void BT_khoamoitrongLop_Click(object sender, EventArgs e)
        {
            GB_Login.Visible = false;
            GB_Main.Visible = true;
            GB_TrangChu.Visible = false;
            GB_HR.Visible = false;
            GB_Class.Visible = false;
            GB_Student.Visible = false;
            GB_Course.Visible = true;

        }

        private void BT_doithongtinLop_Click(object sender, EventArgs e)
        {
            try
            {
                if (TB_soluonghvLop.Text == "" || TB_gioihanhvLop.Text == "" || CB_tengiaovien.Text == "" || CB_sobuoihoc.Text == "" || TB_kyhieuLop.Text=="")
                {
                    MessageBox.Show("Nhập Đầy Đủ Thông Tin", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cnUpdateLop = new OleDbConnection();
                    cnUpdateLop.ConnectionString = connection_string;
                    cnUpdateLop.Open();
                    OleDbCommand cmdUpdateTTHV = new OleDbCommand();
                    cmdUpdateTTHV.Connection = cnUpdateLop;
                    int id = int.Parse(TB_maLop.Text.ToString());
                    int limit_student = int.Parse(TB_gioihanhvLop.Text.ToString());
                    int id_teacher = int.Parse(CB_tengiaovien.SelectedValue.ToString());
                    //int belong = int.Parse(TB_khoahocLop.Text.ToString());
                    string ccode = TB_kyhieuLop.Text, time = CB_sobuoihoc.Text;
                    cmdUpdateTTHV.CommandText = " update class set (ccode = @ccode , limit_student =  @limit_student , id_teacher = @id_teacher, name_teacher = @name_teacher , time = @time) where id = @id";
                    cmdUpdateTTHV.Parameters.Add("@id", OleDbType.Integer).Value = id;
                    cmdUpdateTTHV.Parameters.Add("@ccode", OleDbType.VarChar).Value = ccode;
                    cmdUpdateTTHV.Parameters.Add("@limit_student", OleDbType.Integer).Value = limit_student;
                    cmdUpdateTTHV.Parameters.Add("@id_teacher", OleDbType.Integer).Value = id_teacher;
                    cmdUpdateTTHV.Parameters.Add("@name_teacher", OleDbType.VarChar).Value = CB_tengiaovien.Text;
                    cmdUpdateTTHV.Parameters.Add("@time", OleDbType.VarChar).Value = time;
                    cmdUpdateTTHV.ExecuteNonQuery();
                    cnUpdateLop.Close();
                    load_datagrid_tatcalop(sender, e);
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


        private void BT_timkiemLop_Click(object sender, EventArgs e)
        {
            load_datagrid_timkiemLop(sender, e);
        }

//--------------------------------------------------------GB_HR-----------------------------------------------------//
        //-----------------------------------------------------------------------------------------------------//


        private void GB_HR_VisibleChanged(object sender, EventArgs e)
        {
            try
            {
                RBT_giangvienHR_CheckedChanged(sender, e);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void RBT_giangvienHR_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                load_datagrid_giangvien(sender, e);
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
            
        }

        private void load_datagrid_giangvien(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from [teacher] where position = 1";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHR.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_nhanvien(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cn = new OleDbConnection();
                cn.ConnectionString = connection_string;
                OleDbCommand command = new OleDbCommand();
                command.Connection = cn;
                command.CommandText = "select * from [teacher] where position = 0";
                OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachHR.DataSource = dt;
                cn.Close();
                cn.Dispose();
                cn = null;

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
        private void load_datagrid_timkiemGiangvien(object sender, EventArgs e)
        {
            try
            {
                if (TB_timkiemHR.Text == "")
                {
                    MessageBox.Show("Nhập Mã Giảng Viên", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cn = new OleDbConnection();
                    cn.ConnectionString = connection_string;
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = cn;
                    int id = int.Parse(TB_timkiemHR.Text.ToString());
                    command.CommandText = "select * from [teacher] where id = " + id ;
                    OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                    DataTable dt = new DataTable();
                    adapt.Fill(dt);
                    DGV_danhsachHR.DataSource = dt;
                    cn.Close();
                    cn.Dispose();
                    cn = null;

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }


//--------------------------------------------------------GB_COURSE-----------------------------------------------------//
        //-----------------------------------------------------------------------------------------------------//
        
        
        private void GB_Course_VisibleChanged(object sender, EventArgs e)
        {
            load_datagrid_khoahoc(sender, e);
            DTP_ngaybatdauKhoa.Value = DateTime.Now;
            DTP_ngayketthucKhoa.Value = DateTime.Now;
            DTP_ngaykhoitaoKhoa.Value = DateTime.Now;
        }

        private void DGV_danhsachKhoa_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                int i = DGV_danhsachKhoa.CurrentCell.RowIndex;
                TB_maKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[0].Value.ToString();
                TB_tenKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[1].Value.ToString();
                TB_kyhieuKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[2].Value.ToString();
                TB_hocphiKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[3].Value.ToString();
                DTP_ngaybatdauKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[4].Value.ToString();
                DTP_ngayketthucKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[5].Value.ToString();
                DTP_ngaykhoitaoKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[6].Value.ToString();
                TB_sotiethocKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[7].Value.ToString();
                TB_soHVchoKhoa.Text = DGV_danhsachKhoa.Rows[i].Cells[8].Value.ToString();
               
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }

        }
        private void load_datagrid_khoahoc(object sender, EventArgs e)
        {
            try
            {
                OleDbConnection cnkhoa = new OleDbConnection();
                cnkhoa.ConnectionString = connection_string;
                OleDbCommand commandKhoa = new OleDbCommand();
                commandKhoa.Connection = cnkhoa;
                commandKhoa.CommandText = "select * from course";
                OleDbDataAdapter adapt = new OleDbDataAdapter(commandKhoa);
                DataTable dt = new DataTable();
                adapt.Fill(dt);
                DGV_danhsachKhoa.DataSource = dt;
                cnkhoa.Close();
                cnkhoa.Dispose();
                cnkhoa = null;

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }

        private void load_datagrid_timkiemKhoa(object sender, EventArgs e)
        {
            try
            {
                if (TB_timkiemKhoa.Text == "")
                {
                    MessageBox.Show("Nhập Thông Tin Cần Tìm", "Yêu Cầu");
                }
                else
                {
                    OleDbConnection cn = new OleDbConnection();
                    cn.ConnectionString = connection_string;
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = cn;
                    int id = int.Parse(TB_timkiemKhoa.Text.ToString());
                    command.CommandText = "select * from class where id = " + id;
                    OleDbDataAdapter adapt = new OleDbDataAdapter(command);
                    DataTable dt = new DataTable();
                    adapt.Fill(dt);
                    DGV_danhsachKhoa.DataSource = dt;
                    cn.Close();
                    cn.Dispose();
                    cn = null;

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
        }
    
    }
}
